package de.tabuma.soap;

import de.tabuma.soap.logik.*;
import de.tabuma.soap.entity.wsdl.*;
import de.tabuma.soap.entity.xsd.*;

import java.io.*;
import java.util.*;
import java.util.logging.*;
import java.net.*;
import javax.xml.bind.*;

/**
 * Die Klasse wandelt Daten von
 * einer Wsdl Datei in das Wsdl Datenobjekt um.
 * @author $Author: alefen $
 * @version $Revision: 1.2 $
 * @since $Date: 2009/12/30 02:53:55 $
 */
public class WsdlFactory {

    private static final Logger log = Logger.getLogger(WsdlFactory.class.getName());
    
    public static TDefinitions loadWsdlUrl(String url) {
        try {
            URL myUrl = new URL(url);
            SoapHandler soapHandler = new SoapHandler(myUrl);
            TDefinitions wsdl = soapHandler.toWsdlObject();
            log.fine("count:"+wsdl.getAnyTopLevelOptionalElement().size());
            SchemaFacade.substituteUrl(wsdl, url);
            log.fine("count:"+wsdl.getAnyTopLevelOptionalElement().size());
            return wsdl;
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return null;
    }

    public static TDefinitions loadWsdlFile(String file) {
        try {
            File myFile = new File(file);
            SoapHandler soapHandler = new SoapHandler(myFile);
            TDefinitions wsdl = soapHandler.toWsdlObject();
            SchemaFacade.substituteUrl(wsdl, myFile.getPath());
            return wsdl;
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return null;
    }

    public static XsSchema loadXsd(String file) {
        try {
            File myFile = new File(file);
            SoapHandler soapHandler = new SoapHandler(myFile);
            XsSchema xsd = soapHandler.toXsdObject();
            ArrayList<XsSchema> schemaList = new ArrayList<XsSchema>();
            SchemaFacade.substituteUrl(schemaList, myFile.getPath());
            return xsd;
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
        return null;
    }

    public static XsSchema loadXsdUrl(String url) {
        try {
            URL myUrl = new URL(url);
            SoapHandler soapHandler = new SoapHandler(myUrl);
            XsSchema xsd = soapHandler.toXsdObject();
            ArrayList<XsSchema> schemaList = new ArrayList<XsSchema>();
            SchemaFacade.substituteUrl(schemaList, url);
            return xsd;
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String args[]) {

        try {

            String file = "C:\\Users\\alefen\\Documents\\NetBeansProjects\\costumercreditBPEL\\src\\TestSchema.wsdl";
            TDefinitions wsdl = loadWsdlFile(file);

            String url = "http://api.google.com/GoogleSearch.wsdl";
            TDefinitions wsdl1 = loadWsdlUrl(url);
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }

    }

    
}